package com.thinkfit.jy.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

public class SoundPopupWindow extends PopupWindow implements View.OnClickListener, PopupWindow.OnDismissListener, View.OnTouchListener {

    private Activity mActivity;
    private AudioManager mAudioManager;
    private RelativeLayout.LayoutParams mLayoutParams;
    private View view;
    private View mProgressView;
    private int mMaxHeight;
    private int mMaxVolume;
    private int mCurrentStyle;
    private int mNeedChangeProgress, mMinHeight;
    private HashMap<String, Integer> mMap;
    public SoundPopupWindow(Activity activity) {
//        super(LayoutInflater.from(activity).inflate(R.layout.activity_sound_1, null),
//                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mActivity = activity;

        view = LayoutInflater.from(activity).inflate(R.layout.activity_sound_1, null);
        RelativeLayout relativeLayout = view.findViewById(R.id.rl);

//        setTouchable(true);
//        setFocusable(true);
//        setOutsideTouchable(true);
//        setBackgroundDrawable(new ColorDrawable(0));
//        update();
//        backgroundAlpha(0.4f);
//        setAnimationStyle(android.R.style.Animation_Translucent);
//        showAsDropDown(anchor);


        activity.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        view.findViewById(R.id.viewBack).setOnTouchListener(this);
        mProgressView = view.findViewById(R.id.viewProgress);
        mCurrentStyle = DataManager.Companion.getInstance().getMainStyle();
        mLayoutParams = (RelativeLayout.LayoutParams) mProgressView.getLayoutParams();
        mMaxHeight = BaseUtils.StaticParams.dp2px(activity, 112);
        mMinHeight = BaseUtils.StaticParams.dp2px(activity, 10);
        mNeedChangeProgress = BaseUtils.StaticParams.dp2px(activity, 102);

        int currentVolume = getAudioManager().getStreamVolume(AudioManager.STREAM_MUSIC);
        //媒体音量 STREAM_MUSIC    STREAM_RING 铃声音量
        mMaxVolume = getAudioManager().getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        resetProgressView(mMaxVolume - currentVolume, mMaxVolume);

        relativeLayout.setOnClickListener(this);

        this.setContentView(view);
        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#4D000000"));
//        ColorDrawable dw = new ColorDrawable(Color.parseColor("#ffffff"));
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(this);


    }

    public void show() {
        showAtLocation(mActivity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    private void resetProgressView(float y, int viewHeight) {
        if (y < 0) {
            y = 0;
        } else if (y > viewHeight) {
            y = viewHeight;
        }
        float progress = (viewHeight - y) / viewHeight;
        int height = (int) (progress * mMaxHeight);
        if (height > mMaxHeight) {
            height = mMaxHeight;
        } else if (height < mMinHeight) {
            height = mMinHeight;
        }
        mProgressView.setBackgroundResource(height > mNeedChangeProgress ?
                R.drawable.shape_efc638_10 : R.drawable.shape_efc638_10_bottom);
        mLayoutParams.height = height;
        mProgressView.setLayoutParams(mLayoutParams);
    }

    /**
     * 调整音量
     * @param y
     * @param viewHeight
     */
    private void resetVolume(float y, int viewHeight) {
        float progress = (viewHeight - y) / viewHeight;
        int volume = (int) (progress * mMaxVolume + 0.5f);
        if (volume > mMaxVolume) {
            volume = mMaxVolume;
        } else if (volume < 1) {
            volume = 1;
        }
        if (mCurrentStyle == 1 || mCurrentStyle == 3) {
            if (mMap == null) {
                mMap = new HashMap<>();
            }

            mMap.put(EventBusEvent.VOICE_CHANGE, (int) (progress * 100));
            EventBus.getDefault().post(mMap);
        }
        //设置铃声音量
        getAudioManager().setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);


    }

    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mActivity.getWindow().setAttributes(lp);
    }

    private AudioManager getAudioManager() {
        if (mAudioManager == null) {
            mAudioManager = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE);
        }
        return mAudioManager;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl:
                dismiss();
                break;
        }
    }

    @Override
    public void onDismiss() {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                resetProgressView(event.getY(), v.getHeight());
                resetVolume(event.getY(), v.getHeight());
                break;
        }
        return true;
    }
}
